package band.wukong.practice.webservice.kh.sample10.nongen;

import band.wukong.practice.webservice.kh.sample10.BinaryService;
import band.wukong.practice.webservice.kh.sample10.BinaryServiceImplService;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.soap.SOAPBinding;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

/**
 * @author wukong(wukonggg@139.com)
 */
public class ClientTest {
    private static final Logger logger = LoggerFactory.getLogger(ClientTest.class);

    private BinaryService port;

    @BeforeClass
    public void init() throws MalformedURLException {
        BinaryServiceImplService service = new BinaryServiceImplService();
//        port = service.getBinaryServiceImplPort(new MTOMFeature()); // MTOM模式
        //下面这种有点罗嗦
        port = service.getBinaryServiceImplPort();
        BindingProvider provider = (BindingProvider) port;
        SOAPBinding binding = (SOAPBinding) provider.getBinding();
        binding.setMTOMEnabled(true);
    }

    @Test
    public void upload() {
        try {
            DataHandler handler = new DataHandler(new FileDataSource(new File("D:/1.png")));
            port.upload(handler);
            byte[] result = FileUtils.readFileToByteArray(new File("D:/2.png"));
            Assert.assertNotNull(result);
        } catch (IOException e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }

    }

}
